package com.qf.java2105.huangchuang.hotel.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2105.huangchuang.hotel.constant.MessageConstant;
import com.qf.java2105.huangchuang.hotel.dao.IFoodDao;
import com.qf.java2105.huangchuang.hotel.entity.ResultVO;
import com.qf.java2105.huangchuang.hotel.factory.BeanFactory;
import com.qf.java2105.huangchuang.hotel.pojo.Food;
import com.qf.java2105.huangchuang.hotel.pojo.FoodType;
import com.qf.java2105.huangchuang.hotel.service.IFoodService;
import com.qf.java2105.huangchuang.hotel.utils.JdbcUtil;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜品业务层实现
 * @author hcvg
 * @version v1.0
 * @date 2021/09/12
 **/
public class FoodServiceImpl implements IFoodService {
    private IFoodDao foodDao = (IFoodDao) BeanFactory.getBean("foodDao");

    @Override
    public ResultVO<List<Food>> findByFoodName(String foodName) {
        try {
            // 1.判断foodName是否存在
            if (StringUtils.isEmpty(foodName)) {
                foodName = "%%";
            }else {
                foodName = "%" + foodName.trim() + "%";
            }

            // 2.调用dao获取数据  --> List<Map>
            List<Map<String, Object>> mapList = foodDao.findByFoodName(foodName);

            // 3.处理数据   --> List<Food>;
            List<Food> foodList = new ArrayList<>();
            for (Map<String, Object> map : mapList) {
                Integer foodId = (Integer) map.get("foodId");
                Integer typeId = (Integer) map.get("typeId");
                String name = (String) map.get("foodName");
                BigDecimal foodPrice = (BigDecimal) map.get("foodPrice");
                BigDecimal foodMprice = (BigDecimal) map.get("foodMprice");
                String foodImage = (String) map.get("foodImage");
                String foodDesc = (String) map.get("foodDesc");
                String typeName = (String) map.get("typeName");

                Food food = new Food(foodId, typeId, name, foodPrice, foodMprice, foodImage, foodDesc);
                FoodType foodType = new FoodType(typeId, typeName);
                food.setFoodType(foodType);
                foodList.add(food);
            }

            return ResultVO.ok(MessageConstant.QUERY_ALL_FOOD_SUCCESS, foodList);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.QUERY_ALL_FOOD_FAILED);
        }
    }

    @Override
    public ResultVO<String> save(Food food) {
        try {
            JdbcUtil.begin();
            foodDao.save(food);
            JdbcUtil.commit();
            return ResultVO.ok(MessageConstant.ADD_FOOD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtil.rollback();
            return ResultVO.error(MessageConstant.ADD_FOOD_FAILED, e.getMessage());
        }
    }

    @Override
    public ResultVO<Food> findById(Integer id) {
        try {
            Food food = foodDao.findById(id);
            return ResultVO.ok(MessageConstant.QUERY_FOOD_SUCCESS, food);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.QUERY_FOOD_FAILED);
        }
    }

    @Override
    public ResultVO<String> deleteById(Integer id) {
        try {
            foodDao.deleteById(id);
            return ResultVO.ok(MessageConstant.DELETE_FOOD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.DELETE_FOOD_FAILED, e.getMessage());
        }
    }

    @Override
    public ResultVO<String> update(Food food) {
        try {
            foodDao.update(food);
            return ResultVO.ok(MessageConstant.UPDATE_FOOD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.UPDATE_FOOD_FAILED,e.getMessage());
        }
    }

    @Override
    public ResultVO<Food> foodNameExist(String foodName) {
        try {
            Integer foodId = foodDao.foodNameExist(foodName);
            if (null != foodId){
                return ResultVO.error("此菜品已存在，请勿重复上传菜品！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVO.ok("可以上传菜品，请补全菜品信息！");
    }
}
